package com.garmin.tw.garminpcsync.android;

import java.net.URI;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;

import org.xmlrpc.android.XMLRPCClient;
import org.xmlrpc.android.XMLRPCMethod;
import org.xmlrpc.android.XMLRPCMethodCallback;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.garmin.tw.garminpcsync.android.adapter.GarageAdapter;
import com.garmin.tw.garminpcsync.common.domain.xmlrpc.Vehicle;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.Tracker;

public class GarageListViewActivity extends ListActivity {
	
	private URI uri;
	
	private XMLRPCClient client;
	
	private ProgressDialog progressDialog;
	
	private NumberFormat formatter = new DecimalFormat("#,###");
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.uri = URI.create(getString(R.string.server_url));
		
		this.client = new XMLRPCClient(uri);
		
		XMLRPCMethod method = new XMLRPCMethod(client, "APIDispatcher.getGarageList",
			new XMLRPCMethodCallback() {
			public void callFinished(Object result) {
				
				if (result instanceof com.garmin.tw.garminpcsync.common.domain.xmlrpc.GarageResult) {
					com.garmin.tw.garminpcsync.common.domain.xmlrpc.GarageResult garage = (com.garmin.tw.garminpcsync.common.domain.xmlrpc.GarageResult)result;
					
					if (garage.getVehicleList() != null) {
						List<Vehicle> vehicleList = garage.getVehicleList();
						//testResult.setText("VehicleList.size()=" + garage.getVehicleList().size());
						
						/*List<String> descriptionList = new ArrayList<String>();
						List<String> fileSizeList = new ArrayList<String>();
						List<String> thumbnailUrlList = new ArrayList<String>();
						List<String> downloadUrlList = new ArrayList<String>();
						for (int index=0; index<vehicleList.size(); index++) {
							Vehicle vehicle = vehicleList.get(index);
							
							String fileSize = vehicle.getFileSize().trim();
							descriptionList.add(vehicle.getDescription());
							if (fileSize.indexOf(',') > 0) {
								fileSizeList.add(vehicle.getFileSize());
							} else {
								fileSizeList.add(formatter.format(Double.valueOf(fileSize).longValue()));
							}
							thumbnailUrlList.add(vehicle.getThumbnailURL());
							downloadUrlList.add(vehicle.getUrl() + vehicle.getFileName());
						}*/
						
						GarageAdapter adapter = new GarageAdapter(GarageListViewActivity.this, vehicleList);
						
						ListView lv = getListView();
						lv.setAdapter(adapter);
						lv.setTextFilterEnabled(true);
						lv.setOnItemClickListener(new OnItemClickListener() {
							public void onItemClick(AdapterView<?> parent, View view, int position,
									long id) {
								// Get current context.
								Context mCtx = GarageListViewActivity.this;
								// setContext will use mCtx to retrieve the application context.
								EasyTracker.getInstance().setContext(mCtx);
								// EasyTracker is now ready for use.
								Tracker myTracker = EasyTracker.getTracker();
								
								TextView description = (TextView) view.findViewById(R.id.description);
								
								myTracker.trackEvent("Garage List Page", "Download Garage", description.getText().toString(), null);
								
								// When clicked, show a toast with the TextView text
								Toast.makeText(getApplicationContext(), description.getText(), Toast.LENGTH_SHORT).show();
							}
						});
					}
					
				}
				
				progressDialog.cancel();
			}
		});
		
		progressDialog = new ProgressDialog(this);
		progressDialog.setMessage("Loading. Please wait...");
		progressDialog.show();
		
		/*
		 * esn
		 * locale
		 */
		Object[] params = { "2000000000", "zh_TW" };
		method.call(params);
	}
	
	@Override
	public void onStart() {
		super.onStart();
		// ... // The rest of your onStart() code.
		EasyTracker.getInstance().activityStart(this); // Add this method.
	}

	@Override
	public void onStop() {
		super.onStop();
		// ... // The rest of your onStop() code.
		EasyTracker.getInstance().activityStop(this); // Add this method.
	}
}
